package com.dom;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *  xpath: 快速定位查找
 *  在DOM对象中,快速找到想要的元素
 *
 *  xpath工具方法
 *     List<Node>selectNodes("xpath查找表达式") 获取多个节点
 *     Node selectSingleNode(xpath查找表达式) 获取一个节点
 *
 *     标签对象接口Element继承Node
 *
 *     搜狗智慧拼音输入法
 *     QQ拼音输入法
 */
public class XPathReadXml{
    public static void main(String[] args) throws Exception{
        SAXReader sax = new SAXReader();
        Document document = sax.read("src/main/java/com/dom4j/student.xml");

        //AAA//BBB//CCC
        Element element =(Element) document.selectSingleNode("//students//student//name//xing");
        System.out.println(element.getText());

        //BBB
        element = (Element)document.selectSingleNode("//ming");
        System.out.println(element.getText());

        element = (Element)document.selectSingleNode("//students//student[last()]//ming[last()]");
        System.out.println(element.getText());

        element = (Element)document.selectSingleNode("//name[@id='itcast2']");
        System.out.println(element.getText());


    }
}
